package com.fenshi.presenter.imp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.fenshi.bean.CardBean;
import com.fenshi.interactor.ScanInteractor;
import com.fenshi.interactor.imp.ScanInteractorImp;
import com.fenshi.manager.R;
import com.fenshi.manager.activity.CarListActivity;
import com.fenshi.manager.activity.RentActivity;
import com.fenshi.manager.activity.backcar.BackActivity;
import com.fenshi.manager.activity.backcar.BackCarOrderActivity;
import com.fenshi.presenter.ScanPresenter;
import com.fenshi.presenter.listener.RequestListener;
import com.fenshi.until.CookicUntil;
import com.fenshi.until.MethodCode;
import com.fenshi.view.ScanView;

/**
 * Created by Administrator on 2015/10/9.
 */
public class ScanPresenterImp implements ScanPresenter,RequestListener<JSONObject>{

    ScanInteractor interactor;
    ScanView view;
    Context c;
    Activity act;
    String cardId;

    public ScanPresenterImp(Context c){
        this.c = c;
        this.act = (Activity) c;
        this.view = (ScanView) c;
        interactor =new ScanInteractorImp(c,this);
    }

    @Override
    public void checkCard(String cardId) {
        view.showLoad();
        if(cardId == null ||cardId.length() == 0 ) {
            view.showError("卡片id不合法");
            return;
        }
        this.cardId = cardId;
        interactor.checkCard(cardId);
    }

    @Override
    public void isRent() {
        Bundle bundle = new Bundle();
        bundle.putString(CookicUntil.CODE,cardId);
        if (act.getIntent().getExtras()!= null && act.getIntent().getExtras().getString(CookicUntil.FROM) !=null) {
            if (act.getIntent().getExtras().getString(CookicUntil.FROM).equals("rentActivty")) {
                Intent intent = new Intent(c, RentActivity.class);
                intent.putExtras(bundle);
                act.setResult(200);
                act.finishAffinity();
                return;
            }
        }else {
            Intent intent = new Intent(c, CarListActivity.class);
            intent.putExtras(bundle);
            act.startActivity(intent);
//            act.finish();
        }
    }

    @Override
    public void isBack(String cardId) {
        Bundle bundle = new Bundle();
        bundle.putString(CookicUntil.CODE,this.cardId);
        if (act.getIntent().getExtras()!= null&& act.getIntent().getExtras().getString(CookicUntil.FROM) !=null) {
            if (act.getIntent().getExtras().getString(CookicUntil.FROM).equals("backActivty")) {
                Intent intent = new Intent(c, BackCarOrderActivity.class);
                intent.putExtras(bundle);
                act.setResult(200);
                act.finishAffinity();
                return;
            }
        }else {
            Intent intent = new Intent(c, BackActivity.class);
            intent.putExtras(bundle);
            act.startActivity(intent);
//            act.finish();
        }

    }

    @Override
    public void Success(String tag, JSONObject result) {
        view.dismissLoad();

        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        int statue = response.getInteger(MethodCode.responseStatue);
        JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
        CardBean bean = JSON.toJavaObject(obj, CardBean.class);
        if (statue == (MethodCode.NetState.SUCCESS.getValue())) {
            view.showRentAlert(bean);
        }else if(statue == (MethodCode.CardStatus.INUSE.getValue())){
            view.showBackAlert(bean);
        }else if(statue == (MethodCode.CardStatus.NOTACCESS.getValue()))
            view.showNotAccessAlert(bean);
        else
            view.showError(MethodCode.CardStatus.getText(statue));

    }

    @Override
    public void Error(String tag, String error) {
        view.dismissLoad();
        view.showError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissLoad();
        view.showError(c.getString(R.string.system_error));
    }
}
